package com.jingdianjichi.auth.application.controller;
import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convert.AuthRoleDTOConverter;
import com.jingdianjichi.auth.application.convert.AuthUserDTOConverter;
import com.jingdianjichi.auth.application.dto.AuthRoleDTO;

import com.jingdianjichi.auth.domain.entity.AuthRoleBO;
import com.jingdianjichi.auth.domain.entity.AuthUserBO;
import com.jingdianjichi.auth.entity.Result;
import com.jingdianjichi.auth.service.AuthRoleDomainService;
import com.jingdianjichi.auth.service.AuthUserDomainService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/role/")
@Slf4j
public class RoleController {
    @Resource
    private AuthRoleDomainService authRoleDomainService ;


    /**
     * 新增角色
     * @param authRoleDTO
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody AuthRoleDTO authRoleDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController {\n.add.dto:{}", JSON.toJSONString(authRoleDTO));
            }

            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleKey()), "角色key不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authRoleDTO.getRoleName()), "角色名称不能为空");


            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOtoBo(authRoleDTO);
            Boolean bean = authRoleDomainService.add(authRoleBO);
            return Result.ok(bean);
        } catch (Exception e) {
            log.error("RoleController {\n.add.error:{}",e.getMessage(),e);
            return Result.fail("新增角色失败");
        }

    }

    /**
     * 修改角色信息
     * @param AuthRoleDTO
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody AuthRoleDTO authRoleDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto:{}", JSON.toJSONString(authRoleDTO));
            }

            Preconditions.checkNotNull(authRoleDTO.getId(),"角色id不能为空");


            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOtoBo(authRoleDTO);
            Boolean bean = authRoleDomainService.update(authRoleBO);
            return Result.ok(bean);
        } catch (Exception e) {
            log.error("UserController.update.error:{}",e.getMessage(),e);
            return Result.fail("修改用户信息失败");
        }

    }

    /**
     * 删除用户
     * @param AuthRoleDTO
     * @return
     */
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody AuthRoleDTO authRoleDTO) {

        try {
            if (log.isInfoEnabled()) {
                log.info("RoleController.delete.dto:{}", JSON.toJSONString(authRoleDTO));
            }

            Preconditions.checkNotNull(authRoleDTO.getId(),"角色id不能为空");
            AuthRoleBO authRoleBO = AuthRoleDTOConverter.INSTANCE.convertDTOtoBo(authRoleDTO);
            Boolean bean = authRoleDomainService.delete(authRoleBO);
            return Result.ok(bean);
        } catch (Exception e) {
            log.error("RoleController.delete.error:{}",e.getMessage(),e);
            return Result.fail("删除用户信息失败");
        }

    }


}
